package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    private final CategoryBrandService categoryBrandService;

    public BrandServiceImpl(CategoryBrandService categoryBrandService) {
        this.categoryBrandService = categoryBrandService;
    }

    @Override
    public PageDTO<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows) {
        // 1.健壮性
        int current = Math.max(page, 1);
        int size = Math.max(rows, 5);
        // SELECT * FROM tb_brand WHERE `name` LIKE "%x%" OR letter = "x"
        // 2.查询结果
        boolean isKeyExists = StringUtils.isNotBlank(key);
        Page<Brand> result = query()
                // 1.1.关键字过滤 WHERE `name` LIKE "%x%" OR letter = "x"
                .like(isKeyExists, "name", key)
                .or()
                .eq(isKeyExists, "letter", key)
                // 1.2.分页
                .page(new Page<>(current, size));
        // 3.解析结果
        long total = result.getTotal();
        long pages = result.getPages();
        List<Brand> list = result.getRecords();
        // 4.返回
        return new PageDTO<>(total, pages, BrandDTO.convertEntityList(list));
    }

    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO) {
        // 1.新增品牌
        Brand brand = brandDTO.toEntity(Brand.class);
        save(brand);
       /* // 2.获取分类id， 包含手机、空调、洗衣机
        List<Long> categoryIds = brandDTO.getCategoryIds();
        // 3.封装中间表对象集合
        Long brandId = brand.getId();// 小米

        List<CategoryBrand> categoryBrands = new ArrayList<>();
        for (Long categoryId : categoryIds) {
            // 创建中间表对象
            CategoryBrand categoryBrand = CategoryBrand.of(categoryId, brandId);
            categoryBrands.add(categoryBrand);
        }*/
        // 2.吧分类id转为中间表对象
        List<CategoryBrand> categoryBrands = brandDTO.getCategoryIds()
                .stream()
                .map(cid -> CategoryBrand.of(cid, brand.getId()))
                .collect(Collectors.toList());
        // 4.新增中间表
        categoryBrandService.saveBatch(categoryBrands);
    }

    @Override
    public List<BrandDTO> queryBrandOfCategory(Long categoryId) {
        List<Brand> list = getBaseMapper().queryByCategoryId(categoryId);
        return BrandDTO.convertEntityList(list);
    }

}
